fn is_leap_year(year: i32) -> bool {
    year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)
}

fn day_of_year(year: i32, month: i32, day: i32) -> i32 {
    let month_days = if is_leap_year(year) {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };
    let mut day_num = 0;
    for m in 0..month - 1 {
        day_num += month_days[m as usize];
    }
    day_num + day
}

fn weekday(year: i32, month: i32, day: i32) -> i32 {
    let (y, m) = if month < 3 {
        (year - 1, month + 10)
    } else {
        (year, month - 2)
    };
    let century = y / 100;
    let year_in_century = y % 100;
    let day_of_week: i32 =
        (day + ((153 * m - 457) / 5) + year_in_century + (year_in_century / 4) + (century / 4)
            - 2 * century)
            % 7;
    if day_of_week < 0 {
        day_of_week + 7
    } else {
        day_of_week
    }
}

fn week_number(year: i32, month: i32, day: i32) -> i32 {
    let day_num = day_of_year(year, month, day);
    let mut first_day_weekday = weekday(year, 1, 1);
    if first_day_weekday == 0 {
        first_day_weekday = 7;
    }
    let week_num = if first_day_weekday > 4 {
        (day_num - 1 + first_day_weekday - 1) / 7
    } else {
        (day_num - 1 + first_day_weekday - 1) / 7 + 1
    };

    if week_num == 53 {
        if weekday(year, 12, 31) < 4 {
            return 1;
        }
    }

    return week_num;
}

fn days_remaining(year: i32, day_num: i32) -> i32 {
    let total_days = if is_leap_year(year) { 366 } else { 365 };
    total_days - day_num
}

fn days_between_dates(
    year1: i32,
    month1: i32,
    day1: i32,
    year2: i32,
    month2: i32,
    day2: i32,
) -> i32 {
    let day_num1 = day_of_year(year1, month1, day1);
    let day_num2 = day_of_year(year2, month2, day2);
    let mut days = 0;
    if year1 < year2 {
        days += day_num2;
        days += if is_leap_year(year1) { 366 } else { 365 };
        for y in year1 + 1..year2 {
            if is_leap_year(y) {
                days += 366;
            } else {
                days += 365;
            }
        }
        days -= day_num1;
    } else {
        days = day_num2 - day_num1;
    }
    days - 1
}

fn get_lunar_new_year(year: i32) -> (i32, i32, i32) {
    match year {
        1899 => (1900, 1, 31),
        1900 => (1901, 2, 19),
        1901 => (1902, 2, 8),
        1902 => (1903, 1, 29),
        1903 => (1904, 2, 16),
        1904 => (1905, 2, 4),
        1905 => (1906, 1, 25),
        1906 => (1907, 2, 13),
        1907 => (1908, 2, 2),
        1908 => (1909, 1, 22),
        1909 => (1910, 2, 10),
        1910 => (1911, 1, 30),
        1911 => (1912, 2, 18),
        1912 => (1913, 2, 6),
        1913 => (1914, 1, 26),
        1914 => (1915, 2, 14),
        1915 => (1916, 2, 3),
        1916 => (1917, 1, 23),
        1917 => (1918, 2, 11),
        1918 => (1919, 2, 1),
        1919 => (1920, 2, 20),
        1920 => (1921, 2, 8),
        1921 => (1922, 1, 28),
        1922 => (1923, 2, 16),
        1923 => (1924, 2, 5),
        1924 => (1925, 1, 24),
        1925 => (1926, 2, 13),
        1926 => (1927, 2, 2),
        1927 => (1928, 1, 23),
        1928 => (1929, 2, 10),
        1929 => (1930, 1, 30),
        1930 => (1931, 2, 17),
        1931 => (1932, 2, 6),
        1932 => (1933, 1, 26),
        1933 => (1934, 2, 14),
        1934 => (1935, 2, 4),
        1935 => (1936, 1, 24),
        1936 => (1937, 2, 11),
        1937 => (1938, 1, 31),
        1938 => (1939, 2, 19),
        1939 => (1940, 2, 8),
        1940 => (1941, 1, 27),
        1941 => (1942, 2, 15),
        1942 => (1943, 2, 5),
        1943 => (1944, 1, 25),
        1944 => (1945, 2, 13),
        1945 => (1946, 2, 2),
        1946 => (1947, 1, 22),
        1947 => (1948, 2, 10),
        1948 => (1949, 1, 29),
        1949 => (1950, 2, 17),
        1950 => (1951, 2, 6),
        1951 => (1952, 1, 27),
        1952 => (1953, 2, 14),
        1953 => (1954, 2, 3),
        1954 => (1955, 1, 24),
        1955 => (1956, 2, 12),
        1956 => (1957, 1, 31),
        1957 => (1958, 2, 18),
        1958 => (1959, 2, 8),
        1959 => (1960, 1, 28),
        1960 => (1961, 2, 15),
        1961 => (1962, 2, 5),
        1962 => (1963, 1, 25),
        1963 => (1964, 2, 13),
        1964 => (1965, 2, 2),
        1965 => (1966, 1, 21),
        1966 => (1967, 2, 9),
        1967 => (1968, 1, 30),
        1968 => (1969, 2, 17),
        1969 => (1970, 2, 6),
        1970 => (1971, 1, 27),
        1971 => (1972, 2, 15),
        1972 => (1973, 2, 3),
        1973 => (1974, 1, 23),
        1974 => (1975, 2, 11),
        1975 => (1976, 1, 31),
        1976 => (1977, 2, 18),
        1977 => (1978, 2, 7),
        1978 => (1979, 1, 28),
        1979 => (1980, 2, 16),
        1980 => (1981, 2, 5),
        1981 => (1982, 1, 25),
        1982 => (1983, 2, 13),
        1983 => (1984, 2, 2),
        1984 => (1985, 2, 20),
        1985 => (1986, 2, 9),
        1986 => (1987, 1, 29),
        1987 => (1988, 2, 17),
        1988 => (1989, 2, 6),
        1989 => (1990, 1, 27),
        1990 => (1991, 2, 15),
        1991 => (1992, 2, 4),
        1992 => (1993, 1, 23),
        1993 => (1994, 2, 10),
        1994 => (1995, 1, 31),
        1995 => (1996, 2, 19),
        1996 => (1997, 2, 7),
        1997 => (1998, 1, 28),
        1998 => (1999, 2, 16),
        1999 => (2000, 2, 5),
        2000 => (2001, 1, 24),
        2001 => (2002, 2, 12),
        2002 => (2003, 2, 1),
        2003 => (2004, 1, 22),
        2004 => (2005, 2, 9),
        2005 => (2006, 1, 29),
        2006 => (2007, 2, 18),
        2007 => (2008, 2, 7),
        2008 => (2009, 1, 26),
        2009 => (2010, 2, 14),
        2010 => (2011, 2, 3),
        2011 => (2012, 1, 23),
        2012 => (2013, 2, 10),
        2013 => (2014, 1, 31),
        2014 => (2015, 2, 19),
        2015 => (2016, 2, 8),
        2016 => (2017, 1, 28),
        2017 => (2018, 2, 16),
        2018 => (2019, 2, 5),
        2019 => (2020, 1, 25),
        2020 => (2021, 2, 12),
        2021 => (2022, 2, 1),
        2022 => (2023, 1, 22),
        2023 => (2024, 2, 10),
        2024 => (2025, 1, 29),
        2025 => (2026, 2, 17),
        2026 => (2027, 2, 6),
        2027 => (2028, 1, 26),
        2028 => (2029, 2, 13),
        2029 => (2030, 2, 3),
        2030 => (2031, 1, 23),
        2031 => (2032, 2, 11),
        2032 => (2033, 1, 31),
        2033 => (2034, 2, 19),
        2034 => (2035, 2, 8),
        2035 => (2036, 1, 28),
        2036 => (2037, 2, 15),
        2037 => (2038, 2, 4),
        2038 => (2039, 1, 24),
        2039 => (2040, 2, 12),
        2040 => (2041, 2, 1),
        2041 => (2042, 1, 22),
        2042 => (2043, 2, 10),
        2043 => (2044, 1, 30),
        2044 => (2045, 2, 17),
        2045 => (2046, 2, 6),
        2046 => (2047, 1, 26),
        2047 => (2048, 2, 14),
        2048 => (2049, 2, 2),
        2049 => (2050, 1, 23),
        2050 => (2051, 2, 11),
        2051 => (2052, 2, 1),
        2052 => (2053, 2, 19),
        2053 => (2054, 2, 8),
        2054 => (2055, 1, 28),
        2055 => (2056, 2, 15),
        2056 => (2057, 2, 4),
        2057 => (2058, 1, 24),
        2058 => (2059, 2, 12),
        2059 => (2060, 2, 2),
        2060 => (2061, 1, 21),
        2061 => (2062, 2, 9),
        2062 => (2063, 1, 29),
        2063 => (2064, 2, 17),
        2064 => (2065, 2, 5),
        2065 => (2066, 1, 26),
        2066 => (2067, 2, 14),
        2067 => (2068, 2, 3),
        2068 => (2069, 1, 23),
        2069 => (2070, 2, 11),
        2070 => (2071, 1, 31),
        2071 => (2072, 2, 19),
        2072 => (2073, 2, 7),
        2073 => (2074, 1, 27),
        2074 => (2075, 2, 15),
        2075 => (2076, 2, 5),
        2076 => (2077, 1, 24),
        2077 => (2078, 2, 12),
        2078 => (2079, 2, 2),
        2079 => (2080, 1, 22),
        2080 => (2081, 2, 9),
        2081 => (2082, 1, 29),
        2082 => (2083, 2, 17),
        2083 => (2084, 2, 6),
        2084 => (2085, 1, 26),
        2085 => (2086, 2, 14),
        2086 => (2087, 2, 3),
        2087 => (2088, 1, 24),
        2088 => (2089, 2, 10),
        2089 => (2090, 1, 30),
        2090 => (2091, 2, 18),
        2091 => (2092, 2, 7),
        2092 => (2093, 1, 27),
        2093 => (2094, 2, 15),
        2094 => (2095, 2, 5),
        2095 => (2096, 1, 25),
        2096 => (2097, 2, 12),
        2097 => (2098, 2, 1),
        2098 => (2099, 1, 21),
        _ => (0, 0, 0),
    }
}

pub fn time_info(time: &str) -> String {
    let parts: Vec<&str> = time.split('-').collect();
    let year: i32 = parts[0].parse().unwrap();
    let month: i32 = parts[1].parse().unwrap();
    let day: i32 = parts[2].parse().unwrap();

    let day_num = day_of_year(year, month, day);
    let week_num = week_number(year, month, day);
    let days_rem = days_remaining(year, day_num);

    // 预定义春节日期
    let mut lunar_new_year = get_lunar_new_year(year - 1);

    let mut days_to_lunar_new_year = days_between_dates(
        year,
        month,
        day,
        lunar_new_year.0,
        lunar_new_year.1,
        lunar_new_year.2,
    );

    if days_to_lunar_new_year < 0 {
        lunar_new_year = get_lunar_new_year(year);
        days_to_lunar_new_year = days_between_dates(
            year,
            month,
            day,
            lunar_new_year.0,
            lunar_new_year.1,
            lunar_new_year.2,
        );
    }

    return format!("{},{},{}", week_num, days_rem, days_to_lunar_new_year);
}
